package org.witsoft.demo.graphql.extend.dataloader;

import lombok.RequiredArgsConstructor;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderOptions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.witsoft.demo.graphql.entity.Author;
import org.witsoft.demo.graphql.repository.AuthorRepository;

import static java.util.concurrent.CompletableFuture.supplyAsync;

@Configuration
@RequiredArgsConstructor
@ConditionalOnBean(CustomGraphQLContextBuilder.class)
public class CustomDataLoaders {

    private final AuthorRepository authorRepository;

    @Bean
    public DataLoader<Long, Author> authorDataLoader(){ return new DataLoader<>(
                ids -> supplyAsync(() -> authorRepository.findAllById(ids)),
                DataLoaderOptions.newOptions().setCachingEnabled(true)
        );
    }

}
